import { IMP3Data } from "./useRequest";

export interface IPieDataByArtists {
	artist: string;
	count: number;
}

export function genPieDataByArtists(data: IMP3Data[]): IPieDataByArtists[] {
	const res: IPieDataByArtists[] = [];
	const pieMap: { [key: string]: number } = {};

	for (const item of data) {
		if (!pieMap[item.artist]) {
			pieMap[item.artist] = 1;
		} else {
			pieMap[item.artist]++;
		}
	}

	return Object.entries(pieMap).map(([artist, count]) => ({ artist, count }));
}

export interface IPieDataByTags {
	tag: string;
	count: number;
}

export function genPieDataByTags(data: IMP3Data[]): IPieDataByTags[] {
	const res: IPieDataByArtists[] = [];
	const pieMap: { [key: string]: number } = {};

	for (const item of data) {
		for (const tag of item.tags) {
			if (!pieMap[tag]) {
				pieMap[tag] = 1;
			} else {
				pieMap[tag]++;
			}
		}
	}

	return Object.entries(pieMap).map(([tag, count]) => ({ tag, count }));
}
